﻿#region Copyrights Notice
//©2009 Microsoft Corporation.  All rights reserved.
#endregion

using System;
using System.Data;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.CISG.BitlockerRecovery.BL;
using Microsoft.Security.Application;

namespace BitlockerRecovery
{
    public partial class AdminFaq : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDD();
                BindFaqDS();
                LoadEmail();
            }
        }
        protected void TopMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            Multiview1.ActiveViewIndex = Int32.Parse(e.Item.Value,CultureInfo.InvariantCulture);

            switch (Multiview1.ActiveViewIndex)
            {
                case 0:
                    BindFaqDS();
                    break;
                case 1:
                    break;
                case 2:
                    LoadArchivalDays();
                    break;
            }

            for (int i = 0; i<= TopMenu.Items.Count - 1; i++ )
            {
                if (i == Int32.Parse(e.Item.Value,CultureInfo.InvariantCulture))
                {
                    //code to change tab selected graphic
                }
                else
                {
                    //code to change tab unselected graphic
                }

            }

        }
        protected void SelectEmail_OnClick(object sender, EventArgs e)
        {
            LoadEmail();
            ltPreview.Text = "";
        }
        protected void LoadEmail()
        {
            string SelectedEmailID = TemplatesList.SelectedValue.ToString();

            DataSet ds =  CommonBL.GetEmailTemplateList();
            ds.Locale = CultureInfo.InvariantCulture;
            DataView dv = new DataView(ds.Tables[0], "NotificationID=" + SelectedEmailID, "NotificationID", DataViewRowState.CurrentRows);

            txtNotificationID.Text = dv[0]["NotificationID"].ToString();
            txtFrom.Text = dv[0]["FromAlias"].ToString();
            txtSubject.Text = dv[0]["Purpose"].ToString();
            txtMessage.Text = AntiXss.GetSafeHtml(dv[0]["Message"].ToString());
        }
        protected void PreviewEmail_OnClick(object sender, EventArgs e)
        {
            ltPreview.Text = AntiXss.GetSafeHtml(txtMessage.Text);
        }
        protected void SaveEmail_OnClick(object sender, EventArgs e)
        {
            Validate("vgEditEmail");
            if (Page.IsValid)
            {
                int SaveNotifID = Int32.Parse(txtNotificationID.Text.ToString(), CultureInfo.InvariantCulture);
                string SaveFrom = txtFrom.Text.ToString();
                string SaveSubject = txtSubject.Text.ToString();
                string SaveMessage = AntiXss.GetSafeHtml(txtMessage.Text.ToString());

                CommonBL.UpdateEmailTemplate(SaveNotifID, SaveFrom, SaveSubject, SaveMessage);

                ltPreview.Text = SaveMessage;
            }
        }

        protected void ArchiveSubmit_OnClick(object sender, EventArgs e)
        {
            Validate("vgEditDays");
            if (Page.IsValid)
            {
                string ConfigName = "ArchivalDays";
                string ConfigValue = txtDays.Text.ToString();
                CommonBL.UpdateConfigValue(ConfigName, ConfigValue);
            }
        }

        protected void LoadDD()
        {
            DataSet ds = CommonBL.GetEmailTemplateList();
            ds.Locale = CultureInfo.InvariantCulture;

            TemplatesList.DataSource = ds;
            TemplatesList.DataBind();
            TemplatesList.SelectedIndex = 0;

        }
        protected void BindFaqDS()
        {
            DataSet ds = CommonBL.GetFaq();
            ds.Locale = CultureInfo.InvariantCulture;
            FaqEdit.DataSource = ds;
            FaqEdit.DataBind();
            Multiview1.ActiveViewIndex = 0;
        }
        protected void FaqEdit_RowCommand(object sender, CommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                Validate("vgAddFaq");
                if (Page.IsValid)
                {
                    int SortOrder = 0;
                    TextBox NewFaq = (TextBox)FaqEdit.FooterRow.FindControl("NewFaq");

                    CommonBL.InsertFaq(SortOrder, AntiXss.GetSafeHtmlFragment(NewFaq.Text));
                    BindFaqDS();
                }
            }
        }  
        protected void FaqEdit_RowEditing(object sender, GridViewEditEventArgs e)
        {
            FaqEdit.EditIndex = e.NewEditIndex;
            BindFaqDS();
        }
        protected void FaqEdit_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            FaqEdit.EditIndex = -1;
            BindFaqDS();
        }
        protected void FaqEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Validate("vgEditFaq");
            if (Page.IsValid)
            {

                int FaqID = Int32.Parse(FaqEdit.DataKeys[e.RowIndex].Values[0].ToString(),CultureInfo.InvariantCulture);
                TextBox txtFaqText = (TextBox)FaqEdit.Rows[e.RowIndex].FindControl("txtEditFaq");
                int SortOrder = 0;
                CommonBL.UpdateFaq(FaqID, SortOrder, AntiXss.GetSafeHtmlFragment(txtFaqText.Text));
                FaqEdit.EditIndex = -1;
                BindFaqDS();
            }
        }
        protected void FaqEdit_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int FaqID = Int32.Parse(FaqEdit.DataKeys[e.RowIndex].Values[0].ToString(),CultureInfo.InvariantCulture);
            CommonBL.DeleteFaq(FaqID);
            FaqEdit.EditIndex = -1;
            BindFaqDS();
        }
        protected void LoadArchivalDays()
        {
            string ArchivalDays = CommonBL.GetConfigValue("ArchivalDays");
            txtDays.Text = ArchivalDays;
            Multiview1.ActiveViewIndex = 2;

        }
    }
}
